#include "StandardICALearn.h"

#include <windows.h>

CStandardICALearn::CStandardICALearn(shared_ptr<CStandardICA> & pNN) :
	m_NN(pNN)
{
}

void CStandardICALearn::Trainning(Vector2 & Input, double learnRate, int itemCount)
{
	int dimension = this->m_NN->m_Dimension;

	Matrix2 X(Input);

	Matrix2 Y(1, dimension);

	Matrix2 Yt(1, dimension);

	Matrix2 Zt(dimension, dimension);

	Matrix2 I(dimension, dimension);
	Matrix2::Identity(I);

	Matrix2 DW(dimension, dimension);
	
	for (int i = 0; i < itemCount; i++)
	{
		Matrix2::Multiply(this->m_NN->m_W, X, Y);
		Matrix2::Transpose(Y, Yt);

		for (int j = 0; j < this->m_NN->m_Dimension; j++)
		{
			double d = *(Y.GetData() + j);
			*(Y.GetData() + j) = 1.0 - 2.0 / (1.0 + ::exp(-1.0 *  d) );
		}

		Matrix2::Multiply(Y, Yt, Zt);

		Zt = Zt + I;

		Zt = Zt * learnRate;

		Matrix2::Multiply(Zt, this->m_NN->m_W, DW);

		this->m_NN->m_W = this->m_NN->m_W + DW;
	}

}